﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;

namespace Freshome.p
{
    internal class Access
    {
    }
    // 主料
    public sealed class MajorawBoy : b.Accesshort<b.Majoraw>
    {
        public MajorawBoy()
        { }
        public List<b.Majoraw> PrimeMajoraws(b.MajorawCateg ct)
        {
            var mjs = p_db.Set<b.Majoraw>().Where(j => j.Recipes.Count(m => m.Prime) > 0).AsNoTracking();
            if (ct != b.MajorawCateg.None)
            {
                return mjs.Where(j => j.Categ == (byte)ct).AsNoTracking().ToList();
            }
            else return mjs.ToList();
        }
        public List<b.Majoraw> SeekMajoraws(string kw)
        {
            return p_db.Set<b.Majoraw>().Where(m => m.Name.Contains(kw) || m.Notes.Contains(kw)).AsNoTracking().ToList();
        }
        public List<b.Majoraw> GetMajoraws(b.RecipeCateg ct)
        {
            if (ct != b.RecipeCateg.None)
            {
                var rts = from m in p_db.Set<b.Majoraw>()
                          join r in p_db.Set<b.Majorecipe>() on m.Mid equals r.Jmx
                          join p in p_db.Set<b.Recipe>() on r.Rmx equals p.Mid
                          where p.Categ == (byte)ct
                          select m;
                return rts.Distinct().AsNoTracking().ToList();
            }
            else return p_db.Set<b.Majoraw>().AsNoTracking().ToList();
        }
    }
    // 调料
    public sealed class MinorawBoy : b.Accesshort<b.Minoraw>
    {
        public MinorawBoy()
        { }
        public List<b.Minoraw> SeekMinoraws(string kw)
        {
            return p_db.Set<b.Minoraw>().Where(m => m.Name.Contains(kw) || m.Notes.Contains(kw)).AsNoTracking().ToList();
        }
        public b.Minoraw Randominoraw()
        {
            var mdict = new Dictionary<short, b.Minoraw>();
            short mnd = 1;
            foreach (var mi in p_db.Set<b.Minoraw>())
            {
                mdict.Add(mnd++, mi);
            }
            if (mdict.Count > 1)
            {
                var rnd = new Random(DateTime.Now.Millisecond);
                return mdict[(short)rnd.Next(1, mdict.Count)];
            }
            else if (mdict.Count == 1)
            {
                return mdict[1];
            }
            else return null;
        }
        public List<b.Minoraw> GetMinoraws(bool usedOnly)
        {
            if (usedOnly)
            {
                return p_db.Set<b.Minoraw>().Where(i => i.Recipes.Count > 0).ToList();
            }
            else return p_db.Set<b.Minoraw>().ToList();
        }
    }
    // 菜谱
    public sealed class RecipeBoy : b.Accesshort<b.Recipe>
    {
        public RecipeBoy()
        { }
        public b.Recipe GetRecipe(short mid, bool withCousin)
        {
            var rp = p_db.Set<b.Recipe>().Include(r => r.Steps).Include(r => r.Majors).Include(r => r.Minors).Include(r => r.Comments)
                .Where(r => r.Mid == mid).AsNoTracking().FirstOrDefault();
            if (rp != null && withCousin)
            {
                var mis = p_db.Set<b.Minoraw>().ToArray();
                var mjs = p_db.Set<b.Majoraw>().ToArray();
                foreach (var ir in rp.Minors)
                {
                    var mi = mis.FirstOrDefault(i => i.Mid == ir.Imx);
                    if (mi != null)
                    {
                        ir.Parentitle = mi.Name;
                        ir.Stamp = mi.Notes;
                    }
                }
                foreach (var jr in rp.Majors)
                {
                    var mj = mjs.FirstOrDefault(j => j.Mid == jr.Jmx);
                    if (mj != null)
                    {
                        jr.Parentitle = mj.Name;
                        jr.Stamp = mj.Notes;
                    }
                }
            }
            return rp;
        }
        public List<b.Recipe> SeekRecipes(string kw)
        {
            var rps = p_db.Set<b.Recipe>().Where(r => r.Name.Contains(kw) || r.Notes.Contains(kw) || r.Steps.Any(p => p.Notes.Contains(kw)))
                .Include(r => r.Majors).Include(r => r.Minors).AsNoTracking().ToList();
            return rps;
        }
        public List<b.Recipe> GetRecipes(bool withMajors)
        {
            if (withMajors)
            {
                return p_db.Set<b.Recipe>().Where(r => r.Majors.Count() > 0).Include(r => r.Majors).OrderByDescending(r => r.Mid).AsNoTracking().ToList();
            }
            else return p_db.Set<b.Recipe>().Where(r => r.Majors.Count() > 0).OrderByDescending(r => r.Mid).AsNoTracking().ToList();
        }
        public List<b.Majoraw> UsedMajoraws()
        {
            return p_db.Set<b.Majoraw>().Where(j => j.Recipes.Count > 0).AsNoTracking().ToList();
        }
        public List<b.Recipe> GetMajorecipes(short mid)
        {
            var rts = from m in p_db.Set<b.Majoraw>()
                      join r in p_db.Set<b.Majorecipe>() on m.Mid equals r.Jmx
                      join p in p_db.Set<b.Recipe>() on r.Rmx equals p.Mid
                      where m.Mid == mid
                      select p;
            return rts.Distinct().AsNoTracking().ToList();
        }
        public List<b.Recipe> GetMinorecipes(short mid)
        {
            var rts = from m in p_db.Set<b.Minoraw>()
                      join r in p_db.Set<b.Minorecipe>() on m.Mid equals r.Imx
                      join p in p_db.Set<b.Recipe>() on r.Rmx equals p.Mid
                      where m.Mid == mid
                      select p;
            return rts.Distinct().AsNoTracking().ToList();
        }
        public List<b.Recipe> GetRecipes(b.RecipeCateg rct, short mid)
        {
            if (rct != b.RecipeCateg.None)
            {
                if (mid > default(short))
                {
                    var rts = from m in p_db.Set<b.Majoraw>()
                              join r in p_db.Set<b.Majorecipe>() on m.Mid equals r.Jmx
                              join p in p_db.Set<b.Recipe>() on r.Rmx equals p.Mid
                              where p.Categ == (byte)rct && m.Mid == mid
                              select p;
                    return rts.Distinct().AsNoTracking().ToList();
                }
                else
                {
                    var rts = from m in p_db.Set<b.Majoraw>()
                              join r in p_db.Set<b.Majorecipe>() on m.Mid equals r.Jmx
                              join p in p_db.Set<b.Recipe>() on r.Rmx equals p.Mid
                              where p.Categ == (byte)rct
                              select p;
                    return rts.Distinct().AsNoTracking().ToList();
                }
            }
            else
            {
                if (mid > default(short))
                {
                    var rts = from m in p_db.Set<b.Majoraw>()
                              join r in p_db.Set<b.Majorecipe>() on m.Mid equals r.Jmx
                              join p in p_db.Set<b.Recipe>() on r.Rmx equals p.Mid
                              where m.Mid == mid
                              select p;
                    return rts.Distinct().AsNoTracking().ToList();
                }
                else
                {
                    var rts = from m in p_db.Set<b.Majoraw>()
                              join r in p_db.Set<b.Majorecipe>() on m.Mid equals r.Jmx
                              join p in p_db.Set<b.Recipe>() on r.Rmx equals p.Mid
                              select p;
                    return rts.Distinct().AsNoTracking().ToList();
                }
            }
        }
        public List<b.Recipe> PrimeRecipes(b.MajorawCateg mct, short mid)
        {
            if (mct != b.MajorawCateg.None)
            {
                if (mid > default(short))
                {
                    var rts = from m in p_db.Set<b.Majoraw>()
                              join r in p_db.Set<b.Majorecipe>() on m.Mid equals r.Jmx
                              join p in p_db.Set<b.Recipe>() on r.Rmx equals p.Mid
                              where m.Categ == (byte)mct && m.Mid == mid && r.Prime
                              select p;
                    return rts.Distinct().AsNoTracking().ToList();
                }
                else
                {
                    var rts = from m in p_db.Set<b.Majoraw>()
                              join r in p_db.Set<b.Majorecipe>() on m.Mid equals r.Jmx
                              join p in p_db.Set<b.Recipe>() on r.Rmx equals p.Mid
                              where m.Categ == (byte)mct && r.Prime
                              select p;
                    return rts.Distinct().AsNoTracking().ToList();
                }
            }
            else
            {
                if (mid > default(short))
                {
                    var rts = from m in p_db.Set<b.Majoraw>()
                              join r in p_db.Set<b.Majorecipe>() on m.Mid equals r.Jmx
                              join p in p_db.Set<b.Recipe>() on r.Rmx equals p.Mid
                              where m.Mid == mid && r.Prime
                              select p;
                    return rts.Distinct().AsNoTracking().ToList();
                }
                else
                {
                    var rts = from m in p_db.Set<b.Majoraw>()
                              join r in p_db.Set<b.Majorecipe>() on m.Mid equals r.Jmx
                              join p in p_db.Set<b.Recipe>() on r.Rmx equals p.Mid
                              where r.Prime
                              select p;
                    return rts.Distinct().AsNoTracking().ToList();
                }
            }
        }
    }
    // 保健
    public sealed class HealtholdBoy : b.Accesshort<b.Healthold>
    {
        public HealtholdBoy()
        { }
        public List<b.Healthold> GetHealtholds(bool withChildren)
        {
            if (withChildren)
            {
                var rts = p_db.Set<b.Healthold>().Include(h => h.Rawithes);
                var mjs = p_db.Set<b.Majoraw>().ToArray();
                foreach (var rt in rts)
                {
                    foreach (var rw in rt.Rawithes)
                    {
                        var mj = mjs.FirstOrDefault(j => j.Mid == rw.Jmx);
                        if (mj != null)
                        {
                            rw.Parentitle = mj.Name;
                            rw.Stamp = mj.Notes;
                        }
                    }
                }
                return rts.ToList();
            }
            else return p_db.Set<b.Healthold>().ToList();
        }
        public b.Healthold Randomate()
        {
            var dict = new Dictionary<short, b.Healthold>();
            short mid = 1;
            foreach (var hh in this.GetHealtholds(true))
            {
                dict.Add(mid++, hh);
            }
            if (dict.Count > 1)
            {
                var rnd = new Random(DateTime.Now.Millisecond);
                return dict[(short)rnd.Next(1, dict.Count)];
            }
            else if (dict.Count == 1)
            {
                return dict[1];
            }
            else return null;
        }
    }
    public sealed class SteplanBoy : b.Accesshort<b.Steplan>
    {
        public SteplanBoy()
        { }
    }
    public sealed class MajorecipeBoy : b.Accesshort<b.Majorecipe>
    {
        public MajorecipeBoy()
        { }
    }
    public sealed class PairawBoy : b.Accesshort<b.Pairaw>
    {
        public PairawBoy()
        { }
    }
    public sealed class RawithealthDog : b.Accesshort<b.Rawithealth>
    {
        public RawithealthDog()
        { }
    }
    public sealed class MinorecipeBoy : b.Accesshort<b.Minorecipe>
    {
        public MinorecipeBoy()
        { }
    }
    public sealed class CommentBoy : b.Multint<b.Comment>
    {
        public CommentBoy()
        { }
    }
}
